/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjs.liyq.dao.impl;

import edu.fjs.liyq.dao.UserDao;
import edu.fjs.liyq.entity.User;
import edu.fjs.liyq.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author danny
 */
public class UserDaoImpl implements UserDao {

    @Override
    public List<User> getAll() {
        List<User> userList = new ArrayList<>();
        try (Connection con = DBUtil.getConFromDatasource()) {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from EASYBUY_USER");
            while (rs.next()) {
                User user = new User();
                user.setAccount(rs.getString("EU_USER_ID"));
                user.setPassword(rs.getString("EU_PASSWORD"));
                user.setName(rs.getString("EU_USER_NAME"));
                String sex = rs.getString("EU_SEX");
                if ("M".equals(sex)) {
                    user.setSex("男");
                } else {
                    user.setSex("女");
                }
                userList.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return userList;
    }

    @Override
    public User selectByAccount(String account) {
        User user = null;
        try (Connection con = DBUtil.getConFromDatasource()) {
            PreparedStatement st = con.prepareStatement("select * from EASYBUY_USER where EU_USER_ID=?");
            st.setString(1, account);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setAccount(account);
                user.setName(rs.getString("EU_USER_NAME"));
                user.setPassword(rs.getString("EU_PASSWORD"));
                user.setEmail(rs.getString("EU_EMAIL"));
                user.setMobile(rs.getString("EU_MOBILE"));
                String sex = rs.getString("EU_SEX");
                if ("M".equals(sex)) {
                    user.setSex("男");
                } else {
                    user.setSex("女");
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }

}
